package com.feihao.zhou.demo01;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorTest2 {
    public static void main(String[] args) {
        /*ThreadPoolExecutor pool = new ThreadPoolExecutor(//AbortPolicy()拒绝策略
                3,//核心线程数量
                5, //最大线程池中的线程数
                2,   //临时线程存活的时间
                TimeUnit.MINUTES,  //临时线程存活的时间单位
                new ArrayBlockingQueue<>(5),  //任务队列
                new ThreadPoolExecutor.AbortPolicy()  //任务的拒绝策略
        );*/

        /*ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,//核心线程数量
                5, //最大线程池中的线程数
                2,   //临时线程存活的时间
                TimeUnit.MINUTES,  //临时线程存活的时间单位
                new ArrayBlockingQueue<>(5),  //任务队列
                new ThreadPoolExecutor.DiscardPolicy()  //丢弃任务，但是不抛出异常
        );*/

        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,
                5,
                2,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(5),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        for (int i = 0 ; i < 11; i++) {
            int j = i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"执行了"+j);
                }
            });
        }

        pool.shutdown();
    }
}
